<?php
class CashModel extends Model {
	private static  $read_db=null;	
	/**
	 * 获取read_db的数据库连接
	 */
	private  function  read_db(){
		if(empty($read_db)){
			$read_db=$this->load->database('read_db', TRUE);
		}
		return $read_db;
	}
	
	public function __construct()
    {
        parent::__construct();
//        $this->load->database();
    }

	/**
	 * @param string $uname
	 * @result user
	 */
	function insertAccount($data){
		try {
			$time = time();
			$insertData = array(
				'uid' => $data['uid'],
				'name' => $data['name'],
				'idNo' => $data['idNo'],
				'account' => $data['account'],
				'status' => $data['status'],
				'inputtime' => $time,
				'updatetime' => $time
			);
			$this->db->insert('ca_account', $insertData);		//绑定
			$result = $this->db->insert_id();
		} catch (Exception $e) {
			log_message('error',  $e->getMessage());
		}
		return $result;
	}

	/**
	 * 获取用户绑定支付宝信息
	 * @param $uid
	 * @return mixed
	 */
	function getAccount($uid){
		try {
			$query = $this->db->query('SELECT * FROM ca_account WHERE uid='.$this->db->escape($uid).' and status=2 limit 1');
			$result = $query->row();
		} catch (Exception $e) {
			log_message('error',  $e->getMessage());
		}
		return $result;
	}

	/**
	 * 根据ca_withdraw id获取提现记录信息
	 * @param $id
	 * @return mixed
	 */
	function getwithdrawInfo($id){
		try {
			$query = $this->db->query('SELECT * FROM ca_withdraw WHERE id='.$this->db->escape($id).' and status=1 limit 1');
			$result = $query->row();
		} catch (Exception $e) {
			log_message('error',  $e->getMessage());
		}
		return $result;
	}

	/**
	 * 提现
	 * $data
	 * @param $data
	 * @return bool
	 */
    function operateWithdrawCash($id,$data){
    	try {
			$this->db->trans_strict(FALSE);
			$this->db->trans_begin();

			$time = time();
			//写入提现申请
			$updateData = array(
				'status'=>$data['status'],	//1：申请提现	 2：提现成功	3：提现失败
				'updatetime'=>$time,
			);
			$this->db->where('id', $id);
			$this->db->update('ca_withdraw', $updateData);		//更新提现申请记录表  ca_withdraw
			$isUpdate = $this->db->affected_rows();	//影响记录条数
			if(empty($isUpdate)){$this->db->trans_rollback();return false;}
			if($data['status'] == 2){	//若提现成功写入流水表
				//写入资金流水
				$insertOverFlow = array(
					'uid'=>$data['uid'],
					'type'=>2,	//1：入账   2：出账
					'cash'=>$data['cash']*1000,	//人民币(元)
					'orderid'=>$data['id'],	//提现申请记录id	ca_withdraw id
					'channel'=>'withdraw',	//来源 goods:购买商品  recharge:充值  withdraw:提现  ad：圈发
					'inputtime'=>$time,
					'updatetime'=>$time
				);
				$this->db->insert('ca_overflow', $insertOverFlow);		//插入资金流水记录表  ca_overflow
				$resultOverFlow = $this->db->insert_id();
				if(empty($resultOverFlow)){$this->db->trans_rollback();return false;}
			}
			if($data['status'] == 3){	//提现失败	资金重新写入余额表
				//添加余额
				$cash = $data['cash']*1000;	//人民币兑换成虚拟币
				$this->db->query('update ca_cbalance set balance=balance+'.$this->db->escape($cash).' where uid='.$this->db->escape($data['uid']));
				$updateResult = $this->db->affected_rows();
				if($updateResult < 1){$this->db->trans_rollback();return false;}

				//写入资金流水
				$insertOverFlow = array(
					'uid'=>$data['uid'],
					'type'=>1,	//1：入账   2：出账
					'cash'=>$data['cash']*1000,	//人民币(元)
					'orderid'=>$data['id'],	//提现申请记录id	ca_withdraw id
					'channel'=>'withdraw',	//来源 goods:购买商品  recharge:充值  withdraw:提现  ad：圈发
					'inputtime'=>$time,
					'updatetime'=>$time
				);
				$this->db->insert('ca_overflow', $insertOverFlow);		//插入资金流水记录表  ca_overflow
				$resultOverFlow = $this->db->insert_id();
				if(empty($resultOverFlow)){$this->db->trans_rollback();return false;}
			}
			$this->db->trans_commit();
    	} catch (Exception $e) {
			$this->db->trans_rollback();
    		log_message('error',  $e->getMessage());
    	}
    	 return true;
    }

	/**
	 * 查询今天是否提现过
	 * @param $uid
	 * @return mixed
	 */
	function getIsTxToday($uid){
		try {
			//查询24小时内是否提现过
			$time = time()-(24*3600); //24小时前时间戳
			$query = $this->db->query('select * from ca_withdraw where uid='.$this->db->escape($uid).' and inputtime > '.$time.' limit 1');
			$result=$query->row();
		} catch (Exception $e) {
			log_message('error',  $e->getMessage());
		}
		return $result;
	}

	/**
	 * 获取用户绑定支付宝信息
	 * @param $uid
	 * @param $cash
	 * @return mixed
	 */
	function getBalance($uid,$cash){
		try {
			$money = $cash*1000;	//人民币换成虚拟币
			$query = $this->db->query('SELECT * FROM ca_cbalance WHERE uid='.$this->db->escape($uid).' and balance >='.$this->db->escape($money).' limit 1');
			$result = $query->row();
		} catch (Exception $e) {
			log_message('error',  $e->getMessage());
		}
		return $result;
	}


	/**
	 * 虚拟币商品id
	 * @return mixed
	 */
	function getInventGoods(){
		try {
			$query = $this->db->query("select * from ca_content_1_goods where goods_type = '2' limit 1");
			$result=$query->row();
		} catch (Exception $e) {
			log_message('error',  $e->getMessage());
		}
		return $result;
	}

	/**
	 * 获取用户余额信息
	 * @return mixed
	 */
	function getUbalance($uid){
		try {
			$query = $this->db->query("select * from ca_cbalance where uid = ".$this->db->escape($uid)." limit 1");
			$result=$query->row();
		} catch (Exception $e) {
			log_message('error',  $e->getMessage());
		}
		return $result;
	}

	/**
	 * 获取用户余额信息
	 * @return mixed
	 */
	function getFreezeUbalance($uid){
		try {
			$query = $this->db->query("select sum(cash)*1000 as num from ca_withdraw where uid = ".$this->db->escape($uid)." and status=1 ");
			$result=$query->row();
		} catch (Exception $e) {
			log_message('error',  $e->getMessage());
		}
		return $result;
	}


	/**
	 * 获取用户绑定支付宝状态
	 * @param $uid
	 * @return mixed
	 */
	function getAccountStatus($uid){
		try {
			$query = $this->db->query('SELECT status FROM ca_account WHERE uid='.$this->db->escape($uid).' limit 1');
			$result = $query->row();
		} catch (Exception $e) {
			log_message('error',  $e->getMessage());
		}
		return $result;
	}



    
}